<?php
namespace App\Http\Controllers\Front;

use App\Service\Front\PayService;
use Illuminate\Http\Request;

class PayWxController1118 extends BaseController
{
    //微信支付

    public function __construct(PayService $service)
    {
        $this->db = $service;
    }

    //发起支付
    public function pay(Request $request)
    {
        $token = $request->post('token', ''); //登录token
        $type = $request->post('type', 1); //1原创需求订单
        $orderno = $request->post('orderno', 0); //订单号
        $price = $this->db->orderPrice($type, $orderno);
        if (!$price) {
            return $this->toJson(-2, '价格错误');
        }
        $uOne = $this->db->userByToken($token);
        if (!$uOne) {
            return $this->toJson(-2, '未登录');
        }
        $uid = $uOne['id'];
        $this->db->wxPayAdd([
            'uid' => $uid, 'from_type' => $type,
            'out_trade_no' => $orderno, 'money' => $price,
        ]); //支付log
        $this->notice($orderno);
        return $this->toJson();
    }

    //通知
    public function notice($orderno)
    {
        $wxData = [
            'orderno' => $orderno,
        ];
        $lastid = $this->db->wxNotice($wxData);
        $res = $this->db->wxNoticeModify($lastid, $orderno);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson();
    }
}